package com.itboye.zhujia.login;

import java.util.Observable;
import java.util.Observer;

import org.apache.http.HttpRequest;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.bean.ResultEntity;
import com.itboye.zhujia.net.CodeErrorException;
import com.itboye.zhujia.net.XErrorListener;
import com.itboye.zhujia.net.XRequestListener;
import com.itboye.zhujia.presenter.UserPresenter;
import com.itboye.zhujia.util.ByAlert;
import com.itboye.zhujia.util.T;
/**
 * 修改密码2（输入验证码）
 * @author 余   焱
 *
 */
public class PasswordActivityTwo extends BaseActivity implements OnClickListener,Observer{
	ImageView close_icon;//返回图片
	TextView add_shap_title_tv;//标题
	TextView pass_two_phone;//显示手机号
	TextView pass_two_ok;//下一步
	
	TextView pass_two_time;//验证码重新发送
	EditText pass_code;//验证码
	
	private TimeCount time;//倒计时
	
	private Intent intent;
	
	private String phone;//接上页面传来的手机号
	private String yzm;//接上页面传来的验证码（暂时收不到信息，验证码从上个页面传过来）
	
	private UserPresenter userPresenter;
	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyCreate() {
		setContentView(R.layout.activity_password_two);
		
		add_shap_title_tv.setText("填写验证码");
		
		time = new TimeCount(60000, 1000, pass_two_time);// 构造CountDownTimer对象
		time.start();
		
		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
		yzm=intent.getStringExtra("yzm");
		
		pass_two_phone.setText(phone);
		pass_code.setText(yzm);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon://返回
			finish();
			break;
		case R.id.pass_two_time://重新获取验证码
			getyzm();
			time.start();
			break;
		case R.id.pass_two_ok://下一步
		//	VerificationYZM();
			Intent intent=new Intent(PasswordActivityTwo.this,PasswordActivityThree.class);
			intent.putExtra("phone", phone);
			intent.putExtra("yzm", yzm);
			startActivity(intent);
			PasswordActivityTwo.this.finish();
			break;

		default:
			break;
		}
		
	}
	
	/**
	 * 验证验证码是否正确
	 */
	private void VerificationYZM() {
		if (pass_code.getText().toString().trim() == null) {
			ByAlert.alert("请输入验证码");
			return;
		}
		userPresenter=new UserPresenter(this);
		userPresenter.verification(phone, "2", yzm);
		
	}
	/**
	 * 获取验证码
	 */
	private void getyzm() {
		userPresenter = new UserPresenter(this);
		userPresenter.send(phone, "2");
		
	}
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if(entity.getEventType()==UserPresenter.verification_success){
					String bean=(String) entity.getData();
					if(bean!=null){
						ByAlert.alert("验证通过");
						Intent intent=new Intent(PasswordActivityTwo.this,RegisterActivityThree.class);
						intent.putExtra("phone", phone);
						intent.putExtra("yzm", yzm);
						startActivity(intent);
						PasswordActivityTwo.this.finish();
					}else{
						ByAlert.alert("验证失败");
					}
				}if(entity.getEventType() == UserPresenter.verification_fail){
					ByAlert.alert("验证码错误");
				}
				if(entity.getEventType()==UserPresenter.send_success){
					String bean=(String) entity.getData();
					if(bean!=null){
						ByAlert.alert("验证码发送成功");
						pass_code.setText(bean);
					}else{
						ByAlert.alert("失败");
					}
				}if(entity.getEventType() == UserPresenter.send_fail){
					ByAlert.alert("验证码发送失败");
				}
			}
		}
	}
	
	
	
	
/*	*//**
	 * 获取验证码
	 *//*
	private void getyzm() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_send_sms")
				.param("mobile", phone)
				.param("code_type", "2")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						T.showLong(PasswordActivityTwo.this, response);
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(PasswordActivityTwo.this, msg);
						} else {
							T.showLong(PasswordActivityTwo.this, exception.toString());
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	*//**
	 * 验证验证码是否正确
	 *//*
	private void VerificationYZM() {
		if (pass_code.getText().toString().trim() == null) {
			T.showLong(PasswordActivityTwo.this, "请输入验证码");
			return;
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Message_checkCode")
				.param("mobile", phone)
				.param("code", pass_code.getText().toString())
				.param("code_type", "2")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						T.showLong(PasswordActivityTwo.this, response);
						Intent intent=new Intent(PasswordActivityTwo.this, PasswordActivityThree.class);
						intent.putExtra("phone", phone);
						intent.putExtra("yzm", pass_code.getText().toString());
						startActivity(intent);
						
					}
				}).errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(PasswordActivityTwo.this, msg);
						} else {
							T.showLong(PasswordActivityTwo.this, exception.toString());
						}
						
					}
				}).build();
		
		HttpRequest.getDefaultRequestQueue().add(request);
	}
*/
}
